package com.bkaka.controller;

import com.bkaka.bean.ResponseBean;
import com.bkaka.entity.ExamAnswerHistory;
import com.bkaka.entity.ExamPaper;
import com.bkaka.entity.ExamQuestion;
import com.bkaka.service.ExamAnswerHistoryServer;
import com.bkaka.service.ExamPaperServer;
import com.bkaka.service.ExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by maxiaoqiang on 2018/9/27.
 */
@RestController
@RequestMapping(value = "${api.path}/history")
public class ExamAnswerHistoryController {

    @Autowired
    private ExamAnswerHistoryServer historyServer;

    @Autowired
    private ExamQuestionService questionService;

    @Autowired
    private ExamPaperServer paperServer;

    @ResponseBody
    @PostMapping(value = "/commit/{paperId}", produces = "application/json;charset=UTF-8")
    public ResponseBean create(@PathVariable Integer paperId, @Valid @RequestBody List<ExamAnswerHistory> historyList) {
        // 更新交卷时间
        ExamPaper paper = new ExamPaper();
        paper.setId(paperId);
        paper.setCommitTime(new Date().getTime() / 1000);
        paperServer.update(paper);
        // 更新答题分数
        for (ExamAnswerHistory history : historyList) {
            ExamAnswerHistory answerHistory = historyServer.findById(history.getId());
            Integer type = questionService.findById(answerHistory.getExamQuestionId()).getType();
            answerHistory.setUserAnswer(history.getUserAnswer());
            if (type == 0 || type == 2) {// 如果是单选题或判断题
                if (answerHistory.getExamAnswer().equals(history.getUserAnswer())) {
                    answerHistory.setUserScore(Double.parseDouble(answerHistory.getQuestionScore().split("/")[1]));
                }
            } else if (type == 1) {// 如果是多选题
                String[] options = answerHistory.getExamAnswer().split(",");
                String[] split = new ArrayList<String>().toArray(new String[0]);
                if(history.getUserAnswer()!=null){
                    split = history.getUserAnswer().split(",");
                }
                // 当前答对的个数
                int target = 0;
                // 标记是否得分
                int flag= 1;
                // 判断用的答案是否在标准答案内
                for (String answer : split) {
                    if(answerHistory.getExamAnswer().indexOf(answer) == -1){
                        flag = 0;
                    }
                    for (String option : options) {
                        if (answer.equals(option)) {
                            target++;
                        }
                    }
                }
                // 如果选择正确
                if (target == options.length) {
                    answerHistory.setUserScore(Double.parseDouble(answerHistory.getQuestionScore().split("/")[1]));
                } else {
                    if(flag==0){
                        answerHistory.setUserScore(0.0);
                    }else{
                        answerHistory.setUserScore(target * Double.parseDouble(answerHistory.getQuestionScore().split("/")[0]));
                    }
                }
            }
            historyServer.update(answerHistory);
        }
        return new ResponseBean(200, "SUCCESS", null);
    }

    @ResponseBody
    @PostMapping(value = "/ignore/{id}", produces = "application/json;charset=UTF-8")
    public ResponseBean ignore(@PathVariable Integer id) {
        ExamAnswerHistory history = new ExamAnswerHistory();
        history.setId(id);
        history.setStatus(1);
        historyServer.update(history);
        return new ResponseBean(200, "success", null);
    }


}
